package liangtWorkSpace.suanfa;

import org.junit.jupiter.api.Test;

public class TestOr {

    /**
     * 找出一个奇数次的数
     */
    @Test
    public void testOr(){
        int[] ints = {1,1,2,2,3};
        int a = 0;
        for (int i = 0; i < ints.length; i++) {
            a = ints[i] ^ a;
        }
        System.out.println(a);
    }

    /**
     * 数组中出现两次
     */
    @Test
    public void testTow(){
        int[] arr = {1,1,2,5,3,8,2,8};

        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }

        int right = eor & (~eor + 1);
        System.out.println(right);
        int onlyOne = 0;

        for (int i = 0; i < arr.length; i++) {
            if ((right & arr[i]) != 0)
            onlyOne ^= arr[i];
        }

        System.out.println("数字："+onlyOne + "第二个数字："+(eor^onlyOne));
    }

    /**
     * 获取一个数二级制几个1
     */
    @Test
    public void get1(){
        int a = 1024;
        int count = 0 >> 1;
        while (a != 0){
            int rO = a &(~a +1);
            count++;
            a ^= rO;
        }
        System.out.println("该数二进制一共："+count+"个1");
    }

    /**
     * 打印出一个数的二进制
     */
    @Test
    public void getByte(){
        int num = 1024;
        int p = num;
        StringBuilder stringBuilder = new StringBuilder();
        while (num!= 0){
            stringBuilder.append(num % 2);
            num >>= 1;
        }
        System.out.println("数字："+p+"的二级制是："+stringBuilder.reverse().toString());
    }
}
